package com.ruoyi.spider.util;


import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public  class SpiderImgUtil {

/*
    @Autowired
    private UploadImgServiceImpl UploadImgService;
*/



    //读取html中的src图片 小刀娱乐网
    public static List<String> getImgSrcXD(String htmlStr) {
        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> picList = new ArrayList<String>();
//		 String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            // Matcher m =
            // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String group = m.group(1);
                StringBuffer stringUrlBuffer = new StringBuffer();
                if(!group.contains("http")){
                    stringUrlBuffer.append("https://www.x6d.com");
                }
                stringUrlBuffer.append(group);
                picList.add(stringUrlBuffer.toString());

            }
        }
        List<String> stringList = delRepeat(picList);
        return stringList;
    }


    //读取html中的img一张图片作为首页图
    public static String getImgOneSrc(String htmlStr) {
        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> picList = new ArrayList<String>();
//		 String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            // Matcher m =
            // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String group = m.group(1);
                //去除图标
                if(group.contains("http")){
                    picList.add(m.group(1));
                }

            }
        }
        if (picList.size()>0 ){
            return picList.get(0);
        }else{
            return "http://img.xianbaovip.com/title5/no";
        }
    }


 /*
    //读取html中的img图片
    public static List<String> getImgSrc(String htmlStr) {
        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> picList = new ArrayList<String>();
//		 String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            // Matcher m =
            // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String group = m.group(1);
                //去除图标
                if(group.contains("http")){
                    picList.add(m.group(1));
                }
            }
        }
        List<String> stringList = delRepeat(picList);
        return stringList;
    }


    //读取html中的img图片
    public static List<String> getImgSrc_http(String htmlStr) {
        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> picList = new ArrayList<String>();
//		 String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            // Matcher m =
            // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String group = m.group(1);
                    picList.add(m.group(1));
            }
        }
        List<String> stringList = delRepeat(picList);
        return stringList;
    }




    //判断当前文本是不是包含淘宝客信息
    public static Boolean judgeHtmlLegal (String htmlStr,String Id) {

        return  new ViolationServiceImpl().selectViolationWoAiWeb(Id, htmlStr);

    }





    //读取html中的img图片爱收集
    public static List<String> getImgSrc1(String htmlStr) {
        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> picList = new ArrayList<String>();
//		 String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*data-url\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            // Matcher m =
            // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
            Matcher m = Pattern.compile("data-url\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String group = m.group(1);
                StringBuffer stringUrlBuffer = new StringBuffer();
                if(!group.contains("http")){
                    stringUrlBuffer.append(constant.aiShouJiwebhImgUrl);
                }

                stringUrlBuffer.append(group);
                picList.add(stringUrlBuffer.toString());

            }
        }
        List<String> stringList = delRepeat(picList);
        return stringList;
    }


    //读取html中的src图片 爱收集
    public static List<String> getImgSrc2(String htmlStr) {
        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> picList = new ArrayList<String>();
//		 String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            // Matcher m =
            // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String group = m.group(1);
                StringBuffer stringUrlBuffer = new StringBuffer();
                if(!group.contains("http")){
                    stringUrlBuffer.append(constant.aiShouJiwebhImgUrl);
                }
                stringUrlBuffer.append(group);
                picList.add(stringUrlBuffer.toString());

            }
        }
        List<String> stringList = delRepeat(picList);
        return stringList;
    }
    //读取html中的src图片 羊毛vip网
    public static List<String> getImgSrc3(String htmlStr) {
        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> picList = new ArrayList<String>();
//		 String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<a.*href\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            // Matcher m =
            // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
            Matcher m = Pattern.compile("href\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String group = m.group(1);
                StringBuffer stringUrlBuffer = new StringBuffer();
//                if(!group.contains("http")){
//                    stringUrlBuffer.append(constant.aiShouJiwebhImgUrl);
//                }
                stringUrlBuffer.append(group);
                picList.add(stringUrlBuffer.toString());

            }
        }
        List<String> stringList = delRepeat(picList);
        return stringList;
    }*/
    // Set去重并保持list原先顺序的两种方法
    public static List<String> delRepeat(List<String> list) {
      return  new ArrayList<String>(new LinkedHashSet<String>(list));
    }



}
